<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="main-search">
      <el-form-item label="批次号" prop="pc">
        <el-input
          v-model="queryParams.pc"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="成功数" prop="sucessCnt">
        <el-input
          v-model="queryParams.sucessCnt"
          placeholder="请输入成功数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="失败数据" prop="errorCnt">
        <el-input
          v-model="queryParams.errorCnt"
          placeholder="请输入失败数据"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表名称" prop="tabName">
        <el-input
          v-model="queryParams.tabName"
          placeholder="请输入表名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['log:exportLog:add']"
        >导入客户信息
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:exportLog:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exportLogList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="" align="center" prop="id" v-if="true"/> -->
      <el-table-column label="批次号" align="center" prop="pc"/>
      <el-table-column label="总数" align="center" prop="total"/>
      <el-table-column label="成功数" align="center" prop="sucessCnt">
        <template slot-scope="scope">
          <el-tag @click="sucessClick(scope.row)">{{ scope.row.sucessCnt }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="失败数据" align="center" prop="">
        <template slot-scope="scope">
          <el-tag @click="errorClick(scope.row)">{{ scope.row.errorCnt }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="导入时间" align="center" prop="createTime"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="导入客户信息" :visible.sync="log.open" width="90%" append-to-body>
      <import-data :key="keys" :pc="pc" :flag="flag"></import-data>
    </el-dialog>
    <!-- 添加或修改导入记录对话框 -->
    <el-dialog title="导入客户信息" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?batchLog=' + upload.batchLogo"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        :on-change="fileChange"
        :on-remove="removeFile"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip" v-if="false">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
          <div class="el-upload__tip" slot="tip">
            <span><el-input v-model="upload.batchLogo" placeholder="请输入批次号"/></span>
          </div>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="isLoading" type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listExportLog} from "@/api/exportLog";
import importData from "@/components/ImportData";
import {downloadTemplate} from "@/api/customerPool/customerPool"
import {getToken} from "@/utils/auth";

export default {
  name: "ExportLog",
  components: {importData},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 导入记录表格数据
      exportLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isLoading: false,
      hasFile: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        pc: undefined,
        sucessCnt: undefined,
        errorCnt: undefined,
        tabName: undefined,
      },
      pc: '',
      flag: '',
      // 表单参数
      form: {},
      // 数据导入
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        batchLogo: '',
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/exportLog/importData"
      },
      log: {
        open: false
      },
      keys: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询导入记录列表 */
    getList() {
      this.loading = true;
      listExportLog(this.queryParams).then(response => {
        this.exportLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    sucessClick(row) {
      if (row.sucessCnt == 0) {
        return
      }
      this.keys = Math.random()
      this.pc = row.pc
      this.flag = 0
      this.log.open = true
    },
    errorClick(row) {
      if (row.errorCnt == 0) {
        return
      }
      this.keys = Math.random()
      this.pc = row.pc
      this.flag = 1
      this.log.open = true
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.hasFile = false
      this.isLoading = false
      this.upload.open = true;
      this.upload.batchLogo = '';
    },
    /** 下载模板操作 */
    importTemplate() {
      downloadTemplate().then(response => {
        this.baseDownload(response.msg)
      }).catch(error => {
        console.log(error)
      }).finally(() => {
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log('handleFileSuccess:', response.code)
      this.isLoading = false
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (response && response.code && response.code == '200') {
        this.$alert('操作成功');
      } else {
        this.$alert('导入失败,请重试!');
      }

      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      console.log('hasFile:', this.hasFile)
      if (this.hasFile) {
        this.isLoading = true
        this.$refs.upload.submit();
      } else {
        this.$alert('请上传文件！');
      }
    },
    fileChange(file, fileList) {
      this.hasFile = true
    },
    removeFile(file, fileList) {
      this.hasFile = false
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exportLog/export', {
        ...this.queryParams
      }, `exportLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
